package wyp.library.tools;

import java.lang.reflect.Method;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.telephony.TelephonyManager;

public class WypSystemTool {

	public static final String TAG = WypSystemTool.class.getSimpleName();

	public static int getSdkVersion() {
		return Build.VERSION.SDK_INT;
	}

	/**
	 * @author liding
	 * 判断网络是否可用
	 * @param context 上下文
	 * @return
	 */
	public static boolean isNetworkAvailable(Context context) {
		try {
			ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo netWorkInfo = cm.getActiveNetworkInfo();
			return (netWorkInfo != null && netWorkInfo.isAvailable());// 检测网络是否可用
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 得到版本号
	 * @param __context
	 * @return
	 */
	public static int getAppVersionCode(Context __context) {
		try {
			PackageManager __packageManager = __context.getPackageManager();
			PackageInfo __packInfo = __packageManager.getPackageInfo(__context.getPackageName(), 0);
			return __packInfo.versionCode;
		} catch (NameNotFoundException e) {
			return -1;
		}
	}

	/**
	 * 得到版本名称
	 * @param __context
	 * @return
	 */
	public static String getAppVersionName(Context __context) {
		try {
			PackageManager __packageManager = __context.getPackageManager();
			PackageInfo __packInfo = __packageManager.getPackageInfo(__context.getPackageName(), 0);
			return __packInfo.versionName;
		} catch (NameNotFoundException e) {
			return null;
		}
	}

	public static String getPhoneNum(Context _context) {
		TelephonyManager phoneMgr = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE);
		return phoneMgr.getLine1Number();
	}

	public static String getIMEI(Context _context) {
		TelephonyManager _manager = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE);
		String _imei = _manager.getDeviceId(); // 取出IMEI
		return _imei;
	}

	public static String getICCID(Context _context) {
		TelephonyManager _manager = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE);
		String _iccid = _manager.getSimSerialNumber(); // 取出ICCID
		return _iccid;
	}

	public static String getIMSI(Context _context) {
		TelephonyManager _manager = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE);
		String _imsi = _manager.getSubscriberId(); // 取出IMSI
		return _imsi;
	}

	public static boolean isPad(Context _context) {
		if (android.os.Build.VERSION.SDK_INT >= 11) {
			Configuration con = _context.getResources().getConfiguration();
			try {
				Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast", int.class);
				return (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}
		return false;
	}

	public static Intent call_1(String _phoneNum) {

		return new Intent(Intent.ACTION_CALL_BUTTON, Uri.parse("tel:" + _phoneNum));
	}

	public static Intent call_2(String _phoneNum) {
		return new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + _phoneNum));
	}

	/**
	 * 得到设备的唯一Id
	 * @return
	 */
	public static String getInstallationId() {
		return android.os.Build.SERIAL;
	}

}
